package com.youfang.sixthmedia.ui.center;

import android.os.Bundle;
import android.view.ContextMenu;
import android.view.ContextMenu.ContextMenuInfo;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemLongClickListener;

import com.gzit.common.async.AsynCallback;
import com.gzit.utils.AndroidUtils;
import com.youfang.sixthmedia.R;
import com.youfang.sixthmedia.message.GethousesRequest;
import com.youfang.sixthmedia.message.GethousesResponse;
import com.youfang.sixthmedia.message.center.GetMyCollectRequest;
import com.youfang.sixthmedia.message.center.GetMyCollectResponse;
import com.youfang.sixthmedia.message.collect.RemoveFromCollectRequest;
import com.youfang.sixthmedia.message.collect.RemoveFromCollectResponse;
import com.youfang.sixthmedia.message.model.Building;
import com.youfang.sixthmedia.net.Net;
import com.youfang.sixthmedia.net.Response;
import com.youfang.sixthmedia.ui.BaseBuildingActivity;

public class CFavActivity extends BaseBuildingActivity {

	private Building building;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		this.setContentView(R.layout.c_fav_activity);
		super.onCreate(savedInstanceState);
		init();
	}

	public void init() {

		this.registerForContextMenu(this.listview);
		this.listview.setOnItemLongClickListener(new OnItemLongClickListener() {

			@Override
			public boolean onItemLongClick(AdapterView<?> parent, View view,
					int position, long id) {

				// 0 is header view of listview... 1 is the advertisement
				if (position > 1) {
					building = adapter.getItem(position - 2);
					openContextMenu(listview);
					return true;
				}

				return false;
			}
		});
	}

	@Override
	protected boolean shouldLoginToMakeRequest() {
		return true;

	};

	@Override
	public void onCreateContextMenu(ContextMenu menu, View v,
			ContextMenuInfo menuInfo) {
		super.onCreateContextMenu(menu, v, menuInfo);
		menu.add(0, Menu.FIRST, 0, "取消收藏");
	}

	@Override
	public boolean onContextItemSelected(MenuItem item) {
		super.onContextItemSelected(item);
		switch (item.getItemId()) {
		case Menu.FIRST:
			remoteRemoveFav();
			break;
		}

		return true;
	}

	private void remoteRemoveFav() {

		RemoveFromCollectRequest req = new RemoveFromCollectRequest();
		req.setHouseid(building.id);
		Net.doHttpRemote(this, req, new RemoveFromCollectResponse(),
				new AsynCallback<Response>() {

					@Override
					public void callback(Response r) {
						if (!r.isOk()) {
							AndroidUtils.toastShort("取消收藏失败");
							return;
						}

						adapter.remove(building);
						adapter.notifyDataSetChanged();
						AndroidUtils.toastShort("取消收藏成功");

					}
				});

	}

	@Override
	protected GethousesRequest makeRequest() {
		return new GetMyCollectRequest();
	}

	protected GethousesResponse markeResponse() {
		return new GetMyCollectResponse();
	}
}
